package com.seeyon.apps.kk.manager.datasync;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSONArray;
import com.seeyon.apps.kk.client.PTRWebServiceStub;
import com.seeyon.apps.kk.client.PTRWebServiceStub.GetActivityInfo;
import com.seeyon.apps.kk.client.PTRWebServiceStub.GetActivityInfoResponse;
import com.seeyon.apps.kk.client.PTRWebServiceStub.GetAttendeeInfo;
import com.seeyon.apps.kk.client.PTRWebServiceStub.GetAttendeeInfoResponse;
import com.seeyon.apps.kk.client.PTRWebServiceStub.GetHCPCostInfo;
import com.seeyon.apps.kk.client.PTRWebServiceStub.GetHCPCostInfoResponse;
import com.seeyon.apps.kk.manager.FreemarkFlowManager;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.v3x.services.form.FormFactory;
import com.seeyon.v3x.services.form.FormUtils;
import com.seeyon.v3x.services.form.bean.FormExport;

public class InterfaceForSeep extends AbstractInterfaceForSeep{
	private static final Log log = LogFactory.getLog(InterfaceForSeep.class);
	private FreemarkFlowManager freemarkFlowManager;
	private String beforeGrabDataSqlFtl;
	private String adminLoginName;
	private String templateCode;
	private String addCreateDbXMLFtl;
	
	public String getAddCreateDbXMLFtl() {
		return addCreateDbXMLFtl;
	}

	public void setAddCreateDbXMLFtl(String addCreateDbXMLFtl) {
		this.addCreateDbXMLFtl = addCreateDbXMLFtl;
	}

	public String getAdminLoginName() {
		return adminLoginName;
	}

	public void setAdminLoginName(String adminLoginName) {
		this.adminLoginName = adminLoginName;
	}

	public String getTemplateCode() {
		return templateCode;
	}

	public void setTemplateCode(String templateCode) {
		this.templateCode = templateCode;
	}

	public FreemarkFlowManager getFreemarkFlowManager() {
		return freemarkFlowManager;
	}

	public void setFreemarkFlowManager(FreemarkFlowManager freemarkFlowManager) {
		this.freemarkFlowManager = freemarkFlowManager;
	}

	
	public String getBeforeGrabDataSqlFtl() {
		return beforeGrabDataSqlFtl;
	}

	public void setBeforeGrabDataSqlFtl(String beforeGrabDataSqlFtl) {
		this.beforeGrabDataSqlFtl = beforeGrabDataSqlFtl;
	}
	
	@Override
	public boolean beforeGrabData() {
		if (StringUtils.isNotEmpty(this.beforeGrabDataSqlFtl)) {
			String sql = this.freemarkFlowManager.fltTemplateIntoString(this.beforeGrabDataSqlFtl, null);
			this.jdbcTemplate.update(sql);
		}
		return false;
	}

		//SEEP活动信息
		@Override
		public boolean getDataForWebservices1() {
			try {
				PTRWebServiceStub stub = new PTRWebServiceStub();
				GetActivityInfo getActivityInfo0=new GetActivityInfo();
				GetActivityInfoResponse getActivityInfo = stub.GetActivityInfo(getActivityInfo0);
				String str = getActivityInfo.getGetActivityInfoResult().replaceAll("'", "‘");
				JSONArray json = JSONArray.parseArray(str);
				if (json.size() > 0) {
					log.info("KK:活动信息："+json.size()+"条数据");
					for (int i = 0; i < json.size(); i++) {
						Map<String, Object> map = (Map<String, Object>) json.getJSONObject(i);
						String dataXml = this.freemarkFlowManager.fltTemplateIntoString(addCreateDbXMLFtl, map);
						FormFactory formFactory = (FormFactory) AppContext.getBean("formFactory");
						try {
							FormExport[] formExportData = FormUtils.xmlTransformFormExports(dataXml);
							for (FormExport formExport : formExportData) {
								formFactory.importBusinessFormData(this.adminLoginName, this.templateCode, formExport,null);
							}
						} catch (Exception e) {
							log.error(e);
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return false;
		}

		//参会明细
		@Override
		public boolean getDataForWebservices2() {
			try {
				PTRWebServiceStub stub = new PTRWebServiceStub();
				GetAttendeeInfo getAttendeeInfo4 =new GetAttendeeInfo();
				GetAttendeeInfoResponse getAttendeeInfo = stub.GetAttendeeInfo(getAttendeeInfo4);
				String str1 = getAttendeeInfo.getGetAttendeeInfoResult().replaceAll("'", "‘");
				
				JSONArray json = JSONArray.parseArray(str1);
				if (json.size() > 0) {
					log.info("KK:参会明细："+json.size()+"条数据");
					for (int i = 0; i < json.size(); i++) {
						Map<String, Object> map = (Map<String, Object>) json.getJSONObject(i);
						String dataXml = this.freemarkFlowManager.fltTemplateIntoString(addCreateDbXMLFtl, map);
						FormFactory formFactory = (FormFactory) AppContext.getBean("formFactory");
						try {
							FormExport[] formExportData = FormUtils.xmlTransformFormExports(dataXml);
							for (FormExport formExport : formExportData) {
								formFactory.importBusinessFormData(this.adminLoginName, this.templateCode, formExport,null);
							}
						} catch (Exception e) {
							log.error(e);
						}
					}
				}
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			return false;
		}

		//讲师明细
		@Override
		public boolean getDataForWebservices3() {
			try {
				PTRWebServiceStub stub = new PTRWebServiceStub();
				GetHCPCostInfo getHCPCostInfo2 = new GetHCPCostInfo();
				GetHCPCostInfoResponse getHCPCostInfo = stub.GetHCPCostInfo(getHCPCostInfo2);
				String str2 = getHCPCostInfo.getGetHCPCostInfoResult().replaceAll("'", "‘");
				JSONArray json = JSONArray.parseArray(str2);
				if (json.size() > 0) {
					log.info("KK:讲师明细："+json.size()+"条数据");
					for (int i = 0; i < json.size(); i++ ) {
						Map<String, Object> map = (Map<String, Object>) json.getJSONObject(i);
						String dataXml = this.freemarkFlowManager.fltTemplateIntoString(addCreateDbXMLFtl, map);
						FormFactory formFactory = (FormFactory) AppContext.getBean("formFactory");
						try {
							FormExport[] formExportData = FormUtils.xmlTransformFormExports(dataXml);
							for (FormExport formExport : formExportData) {
								formFactory.importBusinessFormData(this.adminLoginName, this.templateCode, formExport,null);
							}
						} catch (Exception e) {
							log.error(e);
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return false;
		}

}
